/*
 * dsf2flac - http://code.google.com/p/dsf2flac/
 *
 * A file conversion tool for translating dsf dsd audio files into
 * flac pcm audio files.
 *
 * Copyright (c) 2013 by respective authors.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *
 * Acknowledgements
 *
 * Many thanks to the following authors and projects whose work has greatly
 * helped the development of this tool.
 *
 *
 * Sebastian Gesemann - dsd2pcm (http://code.google.com/p/dsd2pcm/)
 * SACD Ripper (http://code.google.com/p/sacd-ripper/)
 * Maxim V.Anisiutkin - foo_input_sacd (http://sourceforge.net/projects/sacddecoder/files/)
 * Vladislav Goncharov - foo_input_sacd_hq (http://vladgsound.wordpress.com)
 * Jesus R - www.sonore.us
 *
 */

#include <boost/timer/timer.hpp>
#include <boost/filesystem.hpp>
#include "dsd_decimator.h"
#include "dsf_file_reader.h"
#include "dsdiff_file_reader.h"
#include <math.h>
#include "cmdline.h"
#include <sstream>

#define flacBlockLen 1024

int aplay_main(int argc, char *argv[], DsdSampleReader* dsr);
int ffplay_main(const char *path);

/**
 * int main(int argc, char **argv)
 *
 * Main
 */
int main(int argc, char **argv)
{
    if(argc < 2) {
        printf("usage:%s file_name", argv[0]);
        exit -1;
    }

    boost::filesystem::path inpath(argv[1]);
    printf("play %s\n", inpath.c_str());

    // pointer to the dsdSampleReader (could be any valid type).
    DsdSampleReader* dsr;

    // create either a reader for dsf or dsd
    if (inpath.extension() == ".dsf" || inpath.extension() == ".DSF")
        dsr = new DsfFileReader((char*)inpath.c_str());
    else if (inpath.extension() == ".dff" || inpath.extension() == ".DFF")
        dsr = new DsdiffFileReader((char*)inpath.c_str());
    else {
        return ffplay_main(inpath.c_str());
    }

    // check reader is valid.
    if (!dsr->isValid()) {
        printf("Error opening DSDFF file!\n");
        printf("%s\n",dsr->getErrorMsg().c_str());
        return ffplay_main(inpath.c_str());
    }

   dsr->dispFileInfo();

    ID3_Tag id3tag = dsr->getID3Tag();
    const Mp3_Headerinfo* headInfo = id3tag.GetMp3HeaderInfo();
    if(headInfo != NULL) {

    }

    int argc_new = 4;
    char* argv_new[4] = {"aplay", (char *)inpath.c_str(), (char*)"-D", (char *)"plughw:0,1"};
    aplay_main(argc_new, argv_new, dsr);

    delete dsr;
    return 0;
}
